<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Common\ConstantUtil;
use Think\Page;
class TableController extends CommonController {

    public function index() {
        import("ORG.Think.Page");
        $obj =  M("gx_table")->order("id desc");
        $obj_list = clone $obj;
        $count  = $obj->count();
        $page   = new Page($count,ConstantUtil::ADMIN_PAGE);
        $show   = $page->show();
        $list   = $obj_list->limit($page->firstRow.",".$page->listRows)->select();
        $this->assign([
            "list"  =>  $list ,
            "class" => "Table" ,
            "page" => $show ,
        ]);
        $this->display("table/index");


    }

    public function addView() {
        $this->assign([
            "class" => "Table" ,
        ]);
        $this->display("table/add");
    }

    public function add(){
        $data = I("post.");
        $result = M("gx_table")->add($data);
        $table = ConstantUtil::tableName($result);
        $sql = <<<sql
CREATE TABLE `{$table}` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='{$data['name']}';
sql;
        M()->execute($sql);
        if(empty($result)) {
            $this->error("添加失败!");
        } else {
            $this->success("添加成功!",U('/Admin/Table/fieldAdd/id/'.$result.''));
        }
    }


    /**
     * 字段显示
     */
    public function fieldAdd() {
        $id = I("id");
        $type_list = ConstantUtil::dataType();
        $this->assign([
            "id"        => $id ,
            "type_list" => $type_list ,
            "class"     => "Table" ,
        ]);
        $this->display("table/field_add");
    }


    /**
     * 已有字段情况下， 添加字段
     */
    public function fieldAdd2() {
        $id = I("id");
        $type_list = ConstantUtil::dataType();
        $this->assign([
            "id"        => $id ,
            "type_list" => $type_list ,
            "class"     => "Table" ,
        ]);
        $this->display("table/field_add2");
    }



    /**
     * 字段编辑显示图
     */
    public function fieldIndex() {
        $id = I("id");
        if(empty($id)) $this->error("参数错误！");
        $list = M("gx_table_field")->where(["gx_table_id"=>$id])->select();
        $this->assign([
            "id"        => $id ,
            "class"     => "Table" ,
            "list"     => $list ,
        ]);
        $this->display("table/field_index");
    }

    /**
     *  字段保存
     */
    public function fieldSuccess() {
        $post = I("");
        $table = M("gx_table")->where(["id"=>$post['id']])->find();
        if(!empty($post)) {
            $sql="";
            $model = M();
            $model->startTrans();
            $date = date('Y-m-d H:i:s');
            try{
                foreach($post['name'] as $key => $val) {
                    if($post['type'][$key] == "decimal") {
                        $field = $post['type'][$key]."(".$post['len'][$key].",".$post['decimal'][$key].")";
                    }else{
                        $field = $post['type'][$key]."(".$post['len'][$key].")";
                    }
                    if(empty($sql)) {
                        $sql .= "ALTER TABLE  ".ConstantUtil::tableName($table['id'])."  ADD ".$post['field'][$key]."  ".$field."  COMMENT '".$post['remark'][$key]."'  ";
                    }else{
                        $sql .= ", ADD ".$post['field'][$key]."  ".$field."   COMMENT '".$post['remark'][$key]."' ";
                    }
                    $model->table("gx_table_field")->add([
                        "name"          => $post["name"][$key],
                        "field"         => $post["field"][$key],
                        "remark"        => $post["remark"][$key],
                        "type"          => $post["type"][$key],
                        "len"           => $post["len"][$key],
                        "decimal"       => $post["decimal"][$key],
                        "create_time"   => $date,
                        "gx_table_id"   => $post['id'],
                    ]);
                }
                $model->execute($sql);
                $model->commit();
                $this->success("保存成功",U('/Admin/Table/index'));
            }catch (\PDOException $e) {
                $model->rollback();
                $this->error("保存失败");
            }
        }
    }

    public function info() {
        $id = I("get.id");
        if(empty($id)) $this->error("参数错误!");
        $list = M("gx_table")->where(["id"=>$id])->find();
        $this->assign([
            "list"  =>  $list ,
             "class" => "Table" ,
        ]);
        $this->display("table/info");
    }

    public function editorView(){
        $id = I("get.id");
        if(empty($id)) $this->error("参数错误!");
        $list = M("gx_table")->where(["id"=>$id])->find();
        $this->assign([
            "list"=>$list ,
            "class" => "Table" ,
        ]);
        $this->display("table/editor");
    }

    public function editor(){
        $data = I("post.");
        $id = $data["id"];
        unset($data["id"]);
        $result = M("gx_table")->where(["id"=>$id])->save($data);
        if(empty($result)) {
            $this->error("保存失败!");
        } else {
            $this->success("保存成功!");
        }
    }
    public function delete(){

        $id = I("get.id");
        if(empty($id)) $this->error("参数错误!");
        $result = M("gx_table")->where(["id"=>$id])->delete();
        if(empty($result)) {
            $this->error("删除失败!");
        } else {
            $this->success("删除成功!");
        }
    }
}
